#imports Pyata library
import sys
import gdb
sys.path.append("/home/stefan/Documents/fydp/fydp-david/module-test/fydp-david/src")
from Pd import *

print "D.A.V.I.D. Modules Loaded..."

#creates an instance of Pd
pd = None
#pd = Pd()
    
#initializes Pyata
#pd.init()

lastRangeStart = -1

ranges = {}

def clear():
    global pd

    if pd != None:
        pd.clear()
        pd.save()

def quit():
    global pd

    if pd != None:
        pd.clear()
        pd.quit()
        pd = None

def playSound(pitch, volume,duration,channel):
    global pd
    
    if pd == None:
        print "Initializing pyata_module..."
        pd = Pd()
        pd.init()

    osc = Object(300, 100, "osc~ " + str(pitch))
    vol = Object(300, 200, "*~ " + str(volume))
    dac = Object(300, 300, "dac~")
        
    if channel == 'l' or channel == 'b':
        connect(vol, 0, dac, 0)

    if channel == 'r' or channel == 'b':
        connect(vol, 0, dac, 1)

    connect(osc, 0, vol, 0)
    
    sleep(duration/1000)
    
    #disconnect(vol, 0, dac, 0)
    #disconnect(vol, 0, dac, 1)
    #disconnect(osc, 0, vol, 0)

    osc.delete()
    vol.delete()
    dac.delete()

    #sleep(duration/2000)    


def startRange(startNumber, pitch, volume):
    global pd
    global ranges

    if pd == None:
	print "Initializing pyata_module..."
        pd = Pd()
        pd.init()
    
    if pitch == 0:
	bang = Message(100,300,"bang")
    	line = Object(100,325,"line~")
        osc = Object(100, 350, "osc~")
	leftChan = Message(250,300,"1")
	rightChan = Message(250,325,"0 15")
        vol = Object(100, 375, "*~ " + str(200*volume))
        dac = Object(100, 400, "dac~")
	bangSpeed = Object(100,425, "metro 200")


        connect(bang, 0, bangSpeed, 0)
	connect(bangSpeed, 0, leftChan, 0)
	connect(bangSpeed, 0, rightChan, 0)
        connect(leftChan, 0, line, 0)
	connect(rightChan, 0, line, 0)
        connect(line, 0, vol, 0)
        connect(vol, 0, dac, 0)
        connect(vol, 0, dac, 1)

        bang.click()

    	ranges[startNumber] = []
    	ranges[startNumber].append(dac)
    	ranges[startNumber].append(osc)
    	ranges[startNumber].append(vol)
    	ranges[startNumber].append(line)
    	ranges[startNumber].append(bang)
	ranges[startNumber].append(leftChan)
	ranges[startNumber].append(rightChan)
	ranges[startNumber].append(bangSpeed)
    else:
    	osc = Object(30*startNumber, 100, "osc~ " + str(pitch))
    	vol = Object(30*startNumber, 200, "*~ " + str(volume))
    	dac = Object(30*startNumber, 300, "dac~")
        
    	connect(vol, 0, dac, 0)
    	connect(vol, 0, dac, 1)
    	connect(osc, 0, vol, 0)

    	ranges[startNumber] = []
    	ranges[startNumber].append(dac)
    	ranges[startNumber].append(osc)
    	ranges[startNumber].append(vol)


def stopRange(startNumber):
    global pd
    global ranges

    if pd == None:
        pd = Pd()
        pd.init()

    disconnect(ranges[startNumber][2], 0, ranges[startNumber][0], 0)
    disconnect(ranges[startNumber][2], 0, ranges[startNumber][0], 1)
    disconnect(ranges[startNumber][1], 0, ranges[startNumber][2], 0)

    for box in ranges[startNumber]:
        box.delete()


    #sleep(0.01)


def setLastRangeStart(num):
    global lastRangeStart
    lastRangeStart = num


def getLastRangeStart():
    global lastRangeStart    
    return lastRangeStart

def exit_handler (event):
    clear()

gdb.events.exited.connect (exit_handler)



